/*
 * $Id: ResourceQueue.java,v 1.9 2009/12/09 16:02:13 oliver Exp $
 *
 * Copyright (C) 2006 General Electric Company. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 *
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */

package com.ge.healthcare.autosc.common.load;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import com.ge.healthcare.autosc.common.ASCLogger;
import com.ge.healthcare.autosc.common.ASCUtil;
import com.ge.healthcare.autosc.common.database.pojo.IActTask;
import com.ge.healthcare.autosc.common.pojo.TaskNResource;

public abstract class ResourceQueue implements IResourceQueue {
	/**
	 * Eclipse generated
	 */
	private static final long serialVersionUID = -7779804090618046093L;
	private Map<String, IResourceType> queue;

	public String toString(){
		StringBuffer sb = new StringBuffer();
		sb.append("ResourceQueue Start: ").append(queue);
		sb.append(": End ResourceQueue");
		return sb.toString();
	}
	public ResourceQueue() {
		queue = new HashMap<String, IResourceType>();
	}

	public void addQueue(String name, IResourceType resourceType) {
		queue.put(name, resourceType);
	}

	public IResourceType getResourceType(String key){
		return queue.get(key);
	}
	
	public boolean consume(TaskNResource task) {
		return queue.get(getKey(task)).consume(task);
	}

	public boolean contains(TaskNResource task) {
		return queue.get(getKey(task)).contains(task);
	}

	public int max(String key) {
		return queue.get(getKey(key)).getMax();
	}
	
	public int expireTime(String key){
		return queue.get(getKey(key)).getExpirationDuration();
	}

	public void release(TaskNResource task) {
		queue.get(getKey(task)).release(task);
	}

	public int size(String key) {
		return queue.get(getKey(key)).getCurrentSize();
	}

	protected abstract String getKey(TaskNResource task);

	protected String getKey(String taskKey) {
		String key = TaskNResource.DEFAULT;
		if (!ASCUtil.isNullOrEmpty(taskKey) && queue.containsKey(taskKey)) {
			key = taskKey;
		}
		return key;
	}

	public Set<String> getKeys() {
		return queue.keySet();
	}

	public Set<Long> getKeys(String key) {
		return queue.get(getKey(key)).getKeys();
		
	}

	public TaskNResource get(String key, Long id) {
		IActTask iActTask = queue.get(getKey(key)).get(id);
		if (iActTask instanceof TaskNResource) {
			return (TaskNResource) iActTask;
		} else {
			StringBuffer msg = new StringBuffer();
			msg.append("Invalid task Object found on resource queue task: ");
			msg.append(iActTask).append(" Removing from Queue");
			ASCLogger.error(msg.toString());
			queue.get(getKey(key)).release(iActTask);
		}
		return null;
	}

	public void copy(IResourceQueue resourceQueue) {
		for (Iterator<String> itrKey = resourceQueue.getKeys().iterator(); itrKey
				.hasNext();) {
			String key = itrKey.next();
			for (Iterator<Long> itrId = resourceQueue.getKeys(key).iterator(); itrId
					.hasNext();) {
				Long id = itrId.next();
				TaskNResource resource = resourceQueue.get(key, id);
				if (!ASCUtil.isNullOrEmptyObject(resource)){
					consume(resource.copy());
				}
			}
		}
	}
}
